﻿/********************************************************
 * 类名： SettlementCategoryDAL.cs
 * 功能： 结算方式数据层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2013-11-27	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using PSS.Entity;
using PSS.Entity.SetManage.AuxiliaryInfo;
using PSS.Utility;

namespace PSS.DAL.SetManage.AuxiliaryInfo
{
    public class SettlementCategoryDAL : DbHelperBase
    {
        public int GetTotal()
        {
            var strSql = new StringBuilder();
            strSql.Append(" select  count(*) AS [Total] FROM PSS_SettlementCategory ");
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["Total"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }
        public IList<SettlementCategoryEntity> GetSettlementCategoryList(Pagination qcmodel)
        {
            var list = new List<SettlementCategoryEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            var start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            var end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by CreateTime) AS idx,SCategoryId,SCategoryName,CreateTime FROM PSS_SettlementCategory ");
            strSql.Append(" WHERE 1=1 ");
            strSql.Append(") AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new SettlementCategoryEntity();
                            if (reader["SCategoryId"] != DBNull.Value)
                                model.SCategoryId = new Guid(reader["SCategoryId"].ToString());
                            model.SCategoryName = reader["SCategoryName"] == obj ? null : Convert.ToString(reader["SCategoryName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        public IList<SettlementCategoryEntity> GetSettlementCategoryListByCombobox()
        {
            var list = new List<SettlementCategoryEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT SCategoryId,SCategoryName FROM PSS_SettlementCategory Order by CreateTime  ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new SettlementCategoryEntity();
                            if (reader["SCategoryId"] != DBNull.Value)
                                model.SCategoryId = new Guid(reader["SCategoryId"].ToString());
                            model.SCategoryName = reader["SCategoryName"] == obj ? null : Convert.ToString(reader["SCategoryName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        public bool ModifySettlementCategory(SettlementCategoryEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE [PSS_SettlementCategory] SET [SCategoryName] = @SCategoryName WHERE [SCategoryId] = @SCategoryId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@SCategoryName", model.SCategoryName);
                    Db.AddInParameter(cmd, "@SCategoryId", model.SCategoryId);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        public bool AddSettlementCategory(SettlementCategoryEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO [PSS_SettlementCategory]([SCategoryId],[SCategoryName]) ");
            strSql.Append(" VALUES(@SCategoryId,@SCategoryName) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@SCategoryId", model.SCategoryId);
                    Db.AddInParameter(cmd, "@SCategoryName", model.SCategoryName);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        public SettlementCategoryEntity GetSettlementCategoryInfo(string id)
        {
            var model = new SettlementCategoryEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT [SCategoryId],[SCategoryName] FROM [PSS_SettlementCategory] where [SCategoryId]=@SCategoryId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {

                try
                {
                    Db.AddInParameter(cmd, "@SCategoryId", id);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["SCategoryId"] != DBNull.Value)
                                model.SCategoryId = new Guid(reader["SCategoryId"].ToString());
                            model.SCategoryName = reader["SCategoryName"] == obj ? null : Convert.ToString(reader["SCategoryName"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }
        public bool RemoveSettlementCategory(string id)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" DELETE FROM [PSS_SettlementCategory] WHERE SCategoryId=@SCategoryId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@SCategoryId", id);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
    }
}
